<template>
  <div id="app">
    <div class="editable-form">
      <h2 class="form-title">个人信息表</h2>
      <form @submit.prevent="submitForm">
        <div class="form-row">
          <label class="form-label" for="name">姓名</label>
          <input class="form-input" id="name" v-model="userInfo.name" type="text" placeholder="请输入姓名" />
        </div>
        <div class="form-row">
          <label class="form-label" for="age">年龄</label>
          <input class="form-input" id="age" v-model="userInfo.age" type="number" placeholder="请输入年龄" />
        </div>
        <div class="form-row">
          <label class="form-label" for="height">身高 (m)</label>
          <input class="form-input" id="height" v-model="userInfo.height" type="number" step="0.01"
            placeholder="请输入身高(m)" />
        </div>
        <div class="form-row">
          <label class="form-label" for="weight">体重 (kg)</label>
          <input class="form-input" id="weight" v-model="userInfo.weight" type="number" placeholder="请输入体重(kg)" />
        </div>
        <div class="form-row">
          <label class="form-label" for="medicalHistory">病史</label>
          <textarea class="form-textarea" id="medicalHistory" v-model="userInfo.medicalHistory"
            placeholder="请输入病史（可选）"></textarea>
        </div>
        <div class="form-actions">
          <button class="form-button" type="button" @click="diagnose">诊断</button>
        </div>
      </form>
      <!-- 调试用，可删除 -->
      <!-- <pre class="form-data">{{ userInfo }}</pre> -->
      <div style="text-align: center; font-size: 20px; margin-top: 20px;" v-show="bmiNum != null">
        {{ userInfo.name }}的BMI为: {{ bmiNum && bmiNum.toFixed(1) }}, 结果：{{ diagnoseRes }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      userInfo: {
        name: '',
        age: null,
        height: null,
        weight: null,
        medicalHistory: '',
      },
      diagnoseRes: '',
      bmiNum: null
    };
  },
  methods: {
    diagnose() {
      // 在这里处理表单提交逻辑，比如发送到后端
      if (!this.userInfo.name.trim()) return alert('请输入姓名！')
      if (this.userInfo.age <= 0) return alert('请检查年龄')
      if (this.height <= 0) return alert('请检查身高！')
      if (this.weight <= 0) return alert('请检查体重')
      this.bmiNum = this.userInfo.weight / Math.pow(this.userInfo.height, 2)
      if (this.bmiNum >= 29.9) {
        this.diagnoseRes = '肥胖'
      } else if (this.bmiNum > 24.9) {
        this.diagnoseRes = '体重过重'
      } else if (this.bmiNum > 18.5) {
        this.diagnoseRes = '正常'
      } else {
        this.diagnoseRes = '体重过轻'
      }

      // 清空表单
      // this.resetForm();
    },
    resetForm() {
      this.userInfo = {
        name: '',
        age: null,
        height: null,
        weight: null,
        medicalHistory: ''
      }
    },
  },
};
</script>

<style scoped>
.editable-form {
  width: 50%;
  margin: 0 auto;
  padding: 20px;
  border: 1px solid #ddd;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.form-title {
  text-align: center;
  margin-bottom: 20px;
  font-size: 24px;
  font-weight: bold;
}

.form-row {
  margin-bottom: 15px;
}

.form-label {
  display: inline-block;
  width: 120px;
  font-weight: bold;
  vertical-align: top;
}

.form-input,
.form-textarea {
  width: calc(100% - 130px);
  padding: 8px;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
}

.form-textarea {
  resize: vertical;
  min-height: 80px;
}

.form-actions {
  text-align: center;
  margin-top: 20px;
}

.form-button {
  padding: 10px 20px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
}

.form-button:hover {
  background-color: #45a049;
}

.form-data {
  margin-top: 20px;
  white-space: pre-wrap;
  /* 保持换行符 */
  word-wrap: break-word;
  /* 防止长单词溢出 */
  background-color: #f9f9f9;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
}
</style>